package ex.safe;

import chap7.RationalNumber;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Stack;
import java.util.StringTokenizer;

public class SocketServer {
    public static void main(String[] args) throws IOException {
        //1.建立一个服务器Socket(ServerSocket)绑定指定端口
        ServerSocket serverSocket=new ServerSocket(8900);
        //2.使用accept()方法阻止等待监听，获得新连接
        Socket socket=serverSocket.accept();
        System.out.println("连接成功");
        //3.获得输入流
        InputStream inputStream=socket.getInputStream();
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
        //获得输出流
        OutputStream outputStream=socket.getOutputStream();
        PrintWriter printWriter=new PrintWriter(outputStream);
        //4.读取用户输入信息
        Comparision sofo = new Comparision();
        String suffix = bufferedReader.readLine();
        if(suffix!=null){
            System.out.println("后缀表达式计算结果是" + sofo.calculator(suffix));
        }

        String result = "后缀表达式计算结果是" + sofo.calculator(sofo.calculator(suffix));
        //给客户一个响应
        String reply=result;
        printWriter.write(reply);
        printWriter.flush();
        //5.关闭资源
        printWriter.close();
        outputStream.close();
        bufferedReader.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }
    static class Comparision {
        Stack<String> stack = new Stack<String>();

        RationalNumber num1,num2,num3;

        public Comparision() {
            stack = new Stack<String>();
        }

        public String calculator(String q)
        {
            String op1, op2, result;
            String a;
            StringTokenizer tokenizer = new StringTokenizer(q);

            while (tokenizer.hasMoreTokens())
            {
                a = tokenizer.nextToken();

                if(a.equals("+") || a.equals("-") || a.equals("*") || a.equals("÷"))
                {
                    // 把栈顶的操作数弹出，转换为RationalNumber类型
                    op2 = stack.pop();
                    num2 = conToRantionalNum(op2);

                    // 继续把栈顶的操作数弹出，转换为RationalNumber类型
                    op1 = stack.pop();
                    num1 = conToRantionalNum(op1);

                    //计算结果
                    result = calculate(num1,a.charAt(0),num2);
                    stack.push(result.toString());
                }
                else
                    stack.push(a);
            }
            result = stack.pop();
            return result;
        }

        public RationalNumber conToRantionalNum (String a)
        {
            String numer,denom;  // numer分子，denom分母


            //  把分数a/b以"/"为标记分割开
            StringTokenizer tokenizer1 = new StringTokenizer(a,"/");
            numer = tokenizer1.nextToken();
            if (tokenizer1.hasMoreTokens())
            //  如果分母不为1
            {
                denom = tokenizer1.nextToken();
                num1 = new RationalNumber(Integer.parseInt(numer), Integer.parseInt(denom));
            }
            else
            //  如果分母为1
            {
                num1 = new RationalNumber(Integer.parseInt(numer), 1);
            }
            return num1;
        }

        // 计算+、-、*、÷
        public String calculate(RationalNumber op1, char operation, RationalNumber op2)
        {
            RationalNumber result = new RationalNumber(0,1);

            switch (operation)
            {
                case '+':
                    result = op1.add(op2);
                    break;
                case '-':
                    result = op1.subtract(op2);
                    break;
                case '*':
                    result = op1.multiply(op2);
                    break;
                case '÷':
                    result = op1.divide(op2);
                    break;
            }
            return result.toString();
        }
    }
}